package com.zz.ecommerce.baseManage.rules;

import com.zz.ecommerce.baseManage.rules.bean.TDroolRule;
import com.zz.ecommerce.baseManage.rules.service.TDroolRuleService;
import com.zz.ecommerce.common.CommonDao;
import com.zz.ecommerce.common.GridRequest;
import com.zz.ecommerce.common.GridResult;
import com.zz.ecommerce.user.UserInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.util.HtmlUtils;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/rules")
public class TDroolRuleController {
    @Autowired
    CommonDao commonDao;
    @Autowired
    TDroolRuleService droolRuleService;
    private final Logger logger = LoggerFactory.getLogger(getClass());

    @RequestMapping(value = "init", method = RequestMethod.GET)
    public String TDroolRuleManager() {
        return "baseManager/rules/tdroolrule_manager";
    }

    @RequestMapping(value = "qryTDroolRulePage", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public GridResult<TDroolRule> qryTDroolRulePage(GridRequest gridRequest, TDroolRule tDroolRule) {
        List<TDroolRule> list = commonDao.pageWhere("T_DROOL_RULE", tDroolRule, gridRequest.getStart(), gridRequest.getEnd(), "", "");
        long count = commonDao.countWhere("T_DROOL_RULE", tDroolRule, "");
        return new GridResult<TDroolRule>(list, gridRequest, count);
    }

    @RequestMapping(value = "saveTDroolRule", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String saveTDroolRule(HttpSession session, TDroolRule tDroolRule) {
        try {
            if (StringUtils.isEmpty(tDroolRule.getRuleOperator())) {
                tDroolRule.setRuleOperator(((UserInfo) session.getAttribute("UserInfo")).getLogName());
            }
            tDroolRule.setRuleContent(HtmlUtils.htmlUnescape(tDroolRule.getRuleContent()).replaceAll("\\</br\\>","\r\n"));
            return droolRuleService.saveDroolRule(tDroolRule);
        } catch (Exception e) {

            logger.error("操作失败:{}", e);
            return "操作失败:" + e.getMessage();
        }
    }

    @RequestMapping(value = "getTDroolRuleDetail", method = {RequestMethod.GET, RequestMethod.POST})
    public String getTDroolRuleDetail(TDroolRule tDroolRule, ModelMap modelMap, String modify) {
        TDroolRule tDroolRuleDetail = (TDroolRule) commonDao.qryOneCommon("T_DROOL_RULE", tDroolRule, "");
        modelMap.put("tDroolRule", tDroolRuleDetail);
        modelMap.put("modify", "false".equals(modify));
        return "baseManager/rules/tdroolrule_detail";
    }

    /**
     * 批量更新状态
     *
     * @param ruleIds
     * @return
     */
    @RequestMapping(value = "updateRuleStatus", method = {RequestMethod.GET, RequestMethod.POST})
    @ResponseBody
    public String updateRuleStatus(String ruleIds, String status) {
        try {
            return droolRuleService.updateRuleStatus(ruleIds, status);
        } catch (Exception e) {
            logger.error("操作失败:{}", e);
            return "操作失败:" + e.getMessage();
        }
    }
}
